/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package protocol;

/**
 *
 * @author Alexx
 */
import appClient.ClientCommand;
import appClient.ConnectClient;
import appClient.Forms;
import appClient.UserComman;
import clientListener.ClientEvent;
import com.google.gson.Gson;
import engine.AdressBooks;
import engine.FullPersone;
import engine.GType;


//import org.apache.log4j.Logger;

public class ClientProtocol {
  public ConnectClient connectClient;
  public Forms ab;
 // private static final Logger log = Logger.getLogger(Protocol.class);
  
  public ClientProtocol ()
  {
    
  }
 
  public ClientProtocol (ConnectClient connectClient)
  {
    this.connectClient = connectClient;
    connectClient.clientProtocol = this;
  }
  
    public String getClient(String str) throws org.json.simple.parser.ParseException
   {
       String type;
       String rety = str;
       
       
       Gson gson = new Gson();
       Command obj = gson.fromJson(str, Command.class);
       type = obj.Type;
       String data = obj.Data;
       String add = obj.Add;
       
       if (type.equals("Connect"))
       {
         connectClient.clilist.setRole(new ClientEvent(connectClient, data, add));
       }
       
       if (type.equals("Get"))
       {
         GType gt = gson.fromJson(data, GType.class);
          if (gt.Type.equals("List"))
           {
             connectClient.clilist.setList(new ClientEvent(connectClient, add));
           }
          if (gt.Type.equals("Person"))
           {
             FullPersone fp = gson.fromJson(add, FullPersone.class);
             connectClient.clilist.loadPersone(new ClientEvent(fp, this, connectClient));
             
           }
          if (gt.Type.equals("PhoneT"))
           {
              connectClient.clilist.typePhone(new ClientEvent(ab, add)); 
           }
          if (gt.Type.equals("AddrT"))
           {
             connectClient.clilist.typeAdr(new ClientEvent(ab, add)); 
           }
          if (gt.Type.equals("Country"))
           {
             connectClient.clilist.country(new ClientEvent(ab, add)); 
           }
          if (gt.Type.equals("City"))
           {
             connectClient.clilist.city(new ClientEvent(ab, add));
           }
          if (gt.Type.equals("Street"))
           {
             connectClient.clilist.street(new ClientEvent(ab, add));
           }
          if (gt.Type.equals("Foto"))
           {
             connectClient.clilist.foto(new ClientEvent(ab, add));
           }
         //log.debug(name);
         rety = "Get";
       }

       return rety;
   }
  
}
